package com.qifenqian.turbot.util

import android.content.Context
import android.content.pm.PackageManager
import android.text.SpannableString
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.View
import android.widget.TextView
import com.qifenqian.turbot.R
import java.text.SimpleDateFormat
import java.util.*

/**
 * @author H
 * @createTime 2018/12/25 09:02
 * @description 项目工具
 * @see
 * @since
 */
object TurbotUtil {

    /**
     * 获取系统版本号
     */
    fun getCurrentVersion(context: Context): String {
        return try {
            val packageInfo = context.packageManager.getPackageInfo(context.packageName, PackageManager.GET_CONFIGURATIONS)
            packageInfo.versionName
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
            ""
        }
    }

    fun getCalendar(dateStr: String, datePattern: String): Calendar? {
        val date: Date?
        val format: SimpleDateFormat?
        val calendar: Calendar? = Calendar.getInstance()
        format = SimpleDateFormat(datePattern)
        try {
            date = format.parse(dateStr)
            calendar!!.time = date
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return calendar
    }

    /**
     * TextView设置部分文本高亮
     */
    private fun TextView.setSpannableText(s: String, lightText: String, onClickListener: View.OnClickListener) {
        val spannableString = SpannableString(s)
        spannableString.setSpan(object : ClickableSpan() {
            override fun onClick(view: View) {
                onClickListener.onClick(view)
            }

            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.color = resources.getColor(R.color.blue_e60080ff)
                ds.isUnderlineText = false
            }
        }, s.indexOf(lightText), s.indexOf(lightText) + lightText.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        movementMethod = LinkMovementMethod.getInstance()
        highlightColor = resources.getColor(R.color.transparent)
        text = spannableString
    }

    /**
     * 获取屏幕宽(px)
     */
    fun getScreenWidth(context: Context): Int {
        return context.resources.displayMetrics.widthPixels
    }

    /**
     * 获取屏幕高(px)
     */
    fun getScreenHeight(context: Context): Int {
        return context.resources.displayMetrics.heightPixels
    }
}



